//
//  WBReteetView.swift
//  SainMicroblog
//
//  Created by chenxinju on 16/5/8.
//  Copyright © 2016年 chenxinju-a2. All rights reserved.
//
/// 转发微博
import UIKit
import SDWebImage
import SnapKit

class WBReteetView: UIView {

    //定义用来更新约束的属性 
    var bottonConstrain: Constraint?
    //定义微博属性模型数据
    var retweetViewModel:WBStatusViewModel? {
    didSet {
            //转发微博内容
         //contentLabel.text = retweetViewModel?.retweeted_String
        //内容中有表情需要匹配
          contentLabel.attributedText = retweetViewModel?.dealAttributeString( retweetViewModel?.retweeted_String)
          //先卸载约束
         bottonConstrain?.uninstall()
                                     //====注意内部已经封装返回配图方法
   if let count = retweetViewModel?.retweet_pic_urls?.count where count == 0 {  //没有配图
            // print("转发无配图")
            picView.hidden = true
        //没有配图更新顶部约束为内容的底部
         self.snp_updateConstraints(closure: { (make) -> Void in
       bottonConstrain = make.bottom.equalTo(contentLabel.snp_bottom).constraint
         })
    
       }else {
            //有配图
            picView.hidden = false
        // print("转发有配图")
         self.snp_updateConstraints(closure: { (make) -> Void in
            bottonConstrain = make.bottom.equalTo(picView.snp_bottom).constraint
          })
    
    //赋值转发配图
    picView.pic_urls = retweetViewModel?.retweet_pic_urls

           }
        }
    }
    
      override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
       }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
      //布局子控件
    func setupUI() {
     // backgroundColor = UIColor.yellowColor()
      addSubview(contentLabel)
      addSubview(picView)
       //contentLabel.text = "转发的信息内容我占个位置"
        
        contentLabel.snp_makeConstraints { (make) -> Void in
          make.top.equalTo(self.snp_top)
          make.left.equalTo(self.snp_left)
          make.right.equalTo(self.snp_right)
        }
        
        
        picView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.contentLabel.snp_bottom)
            make.left.equalTo(self.snp_left).offset(WBPhotoViewMargin)
            //宽和高
            //make.width.height.equalTo(100)
        }
       
        
         //给 绑定bottonConstrain 一个约束
        self.snp_makeConstraints { (make) -> Void in
       bottonConstrain = make.bottom.equalTo(picView.snp_bottom).constraint
        }
    }
    
    //MARK:- 懒加载
    private lazy var contentLabel:UILabel = UILabel(size: 16, color: UIColor.blackColor(), singleLine: false)
    //配图
    private lazy var picView: WBPhotoView = WBPhotoView()

}
